package assdraw.view;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Polygon;

import assdraw.model.Circle;
import assdraw.model.Line;
import assdraw.model.Rect;
import assdraw.model.Shape;
import assdraw.model.ShapeGroup;
import assdraw.model.Triangle;


public class ShapeDrawVisitor implements Visitor {

	private Graphics g;
	private boolean isInSelection;
	
	public ShapeDrawVisitor(Graphics g){
		this.g = g;
	}
	
	/**
	 * Initialize the graphics so we can draw
	 * @param g is the initialized graphics used in DrawingCanvas
	 */
	public void setGraphics(Graphics g){
		this.g = g;
	}
	
	/**
	 * Uses drawOval to draw a circle
	 * @param c is the circle to be drawn
	 */
	public void visit(Circle c){
		if(!isInSelection){
			g.setColor(c.getColor());
		}
		g.drawOval((int)c.getX(), (int)c.getY(), (int)c.getRadius()*2, (int)c.getRadius()*2);
	}
	
	/**
	 * Uses drawLine to draw a line
	 * @param l is the line to be drawn
	 */
	public void visit(Line l){
		if(!isInSelection){
			g.setColor(l.getColor());
		}
		g.drawLine((int)l.getX(), (int)l.getY(), (int)l.getEndX(), (int)l.getEndY());
	}
	
	/**
	 * Uses drawRect to draw a rectangle
	 * @param r is the rectangle to be drawn
	 */
	public void visit(Rect r){
		if(!isInSelection){
			g.setColor(r.getColor());
		}
		g.drawRect((int)r.getX(), (int)r.getY(), (int)r.getWidth(), (int)r.getWidth());
	}
	
	/**
	 * Uses drawPolygon to draw a triangle
	 * @param t is the triangle to be drawn
	 */
	public void visit(Triangle t){
		if(!isInSelection){
			g.setColor(t.getColor());
		}
		Polygon p = new Polygon();
		p.addPoint((int)t.getX(), (int)t.getY());
		p.addPoint((int)t.getX2(), (int)t.getY2());
		p.addPoint((int)t.getX3(), (int)t.getY3());
		g.drawPolygon(p);
	}

	/**
	 * Visits each shape in the shape group
	 * @param shapeGroup the shape group to be drawn
	 */
	public void visit(ShapeGroup shapeGroup) {
		for(Shape s : shapeGroup.getComponents()){
			s.accept(this);
		}
	}

	public void setInSelection(boolean isInSelection) {
		this.isInSelection = isInSelection;
		if(isInSelection){
			g.setColor(Color.RED);
		}
	}

	public boolean isInSelection() {
		return isInSelection;
	}

}
